package com.android.listestagespersey;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import static com.android.listestagespersey.BaseFeedParser.*;

public class RssHandler extends DefaultHandler{
	private List<Stage> stages;
	private Stage currentMessage;
	private StringBuilder builder;
	
	public List<Stage> getStages(){
		return this.stages;
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		super.endElement(uri, localName, name);
		if (this.currentMessage != null){
			if (localName.equalsIgnoreCase(TITRE)){
				currentMessage.setTitre(builder.toString());
			} else if (localName.equalsIgnoreCase(ENTREPRISE)){
				currentMessage.setNomEntreprise(builder.toString());
			} else if (localName.equalsIgnoreCase(DESCRIPTION)){
				currentMessage.setDescription(builder.toString());
			} else if (localName.equalsIgnoreCase(LIEU)){
				currentMessage.setLieu(builder.toString());
			} else if (localName.equalsIgnoreCase(DATEPUB)){
				currentMessage.setDatePub(builder.toString());
			} else if (localName.equalsIgnoreCase(STAGE)){
				stages.add(currentMessage);
			}
			builder.setLength(0);	
		}
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		stages = new ArrayList<Stage>();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
		if (localName.equalsIgnoreCase(STAGE)){
			this.currentMessage = new Stage();
		}
	}
}